/*
** Text-based User Interface Library
** Copyright (C) 2008 Eric Day
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to:** Free Software Foundation, Inc. 
** 51 Franklin Street, Fifth Floor,
** Boston, MA  02110-1301, USA
*/ 

#ifndef _TUI_PRIV_H
#define _TUI_PRIV_H

#include "config.h"

#if HAVE_ERRNO_H
#include <errno.h>
#endif
#if HAVE_PTHREAD_H
#include <pthread.h>
#endif
#include <stdio.h>
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "tui.h"

/* limits on variables */
#define TUI_INPUT_LEN  1024
#define TUI_PROMPT_LEN 32
#define TUI_MSG_LEN    256

/* flags for TUI */
#define TUI_FLAGS_SCROLL       0x00000001
#define TUI_FLAGS_STATUS       0x00000002
#define TUI_FLAGS_INPUT        0x00000004
#define TUI_FLAGS_APP_STATUS   0x00000008
#define TUI_FLAGS_APP_FOCUS    0x00000010
#define TUI_FLAGS_INPUT_THREAD 0x00000020
#define TUI_FLAGS_INPUT_LINE   0x00000040

/* macro to help with error checking */
#define TUI_RET_CHECK(__tui, __check, __func, __errno) { \
    if (__check) \
    { \
        tui_destroy(__tui); \
        if (__errno > 0) \
        { \
            fprintf(stderr, __func ":%d:%s\n", __errno, strerror(__errno)); \
            return __errno; \
        } \
        fprintf(stderr, __func ":%d\n", __errno); \
        return -1; \
    } }

/*
** structs
*/

struct _TUI
{
    struct pollfd  *pfd;
    int             flags;
    char            prompt[TUI_PROMPT_LEN];
    WINDOW         *main_win;
    WINDOW         *scroll_win;
    WINDOW         *status_win;
    WINDOW         *input_win;
    int             input_stdin;
    int             input_pipe[2];
    pthread_t       input_thread_id;
    char            input_buf[TUI_INPUT_LEN];
    int             input_len;
    int             input_curs;
    int             input_state;
    WINDOW         *app_status_win;
    WINDOW         *app_win;
    int             app_win_height;
    void           *app_data;
    int           (*app_input)(TUI *tui, void *data, int key);
    int           (*app_focus)(TUI *tui, void *data);
};

#endif /* _TUI_PRIV_H */
